﻿using System;
using System.Data;
using System.Data.SqlClient;
using HRM.DBGatway;

namespace HRM.Models
{
    public class CodePrefix:DbGateway 
    {
        public int Id { get; set; }
        public string Prefix { get; set; }
        public int Number { get; set; }


         public bool SaveCodePrefix(CodePrefix codePrefix)
         {
             if (HasAnyCodeExists(codePrefix))
             {
                 return UpdateCode(codePrefix);
             }
             else
             {
                 return SaveCode(codePrefix);
             }
         }

        private bool UpdateCode(CodePrefix codePrefix)
        {
            try
            {
                ConnectionObj.Open();
                CommandObj.CommandText = string.Format(" Update tblCodePrefix set Prefix=@prefix,Number=@number");
                CommandObj.Parameters.Clear();
                CommandObj.Parameters.Add("@prefix", SqlDbType.VarChar).Value = codePrefix.Prefix;
                CommandObj.Parameters.Add("@number", SqlDbType.Int).Value = codePrefix.Number;
                CommandObj.ExecuteNonQuery();
                return true;
            }
            catch (Exception)
            {
                throw new Exception("Error Occured, Try again !");
            }
            finally
            {
                ConnectionObj.Close();
            }
        }

        private bool SaveCode(CodePrefix codePrefix)
        {
            try
            {
                ConnectionObj.Open();
                CommandObj.CommandText = string.Format("insert into tblCodePrefix values(@prefix,@number)");
                CommandObj.Parameters.Clear();
                CommandObj.Parameters.Add("@prefix", SqlDbType.VarChar).Value = codePrefix.Prefix;
                CommandObj.Parameters.Add("@number", SqlDbType.Int).Value = codePrefix.Number;
                CommandObj.ExecuteNonQuery();
                return true;
            }
            catch (Exception)
            {
                throw new Exception("Error Occured, Try again !");
            }
            finally
            {
                ConnectionObj.Close();
            }
        }

        private bool HasAnyCodeExists(CodePrefix prefix)
        {
            try
            {
                ConnectionObj.Open();
                CommandObj.CommandText = @"select * from tblCodePrefix";
                SqlDataReader reader = CommandObj.ExecuteReader();
                while (reader.Read())
                {
                    return true;
                }
            }
            catch (Exception)
            {
                throw new Exception("Error Occured, Try again !");
            }
            finally
            {
                ConnectionObj.Close();
            }
            return false;
        }

        public CodePrefix GetCodePrefix()
        {
            try
            {
                ConnectionObj.Open();
                CommandObj.CommandText = @"select * from tblCodePrefix";
                SqlDataReader reader = CommandObj.ExecuteReader();
                CodePrefix prefix=new CodePrefix();
                while (reader.Read())
                {
                    prefix.Prefix = reader[1].ToString();
                    prefix.Number = Convert.ToInt32(reader[2]);
                }
                return prefix;
            }
            catch (Exception)
            {
                throw new Exception("Error Occured, Try again !");
            }
            finally
            {
                ConnectionObj.Close();
            }
         }
    }
}